package com.gqzmy.springboot3.controller.advice;

import com.gqzmy.springboot3.exceptions.ExceptionInfo;
import com.gqzmy.springboot3.exceptions.SystemApiException;
import com.gqzmy.springboot3.exceptions.SystemRuntimeException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * @author 周小哥  gqzmy@163.com 2023/4/20 23:03
 */

@ControllerAdvice
public class ControllerAdviceHandler {

    public static final Logger logger = LoggerFactory.getLogger(ControllerAdviceHandler.class);

    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public ResponseEntity<ExceptionInfo> methodArgumentNotValidException(MethodArgumentNotValidException e) {
        ExceptionInfo build = ExceptionInfo.create()
                .errorMessage(e.getBindingResult().getAllErrors()
                        .stream().map(ObjectError::getDefaultMessage).collect(Collectors.joining(";")))
                .userTip("参数输入不合法")
                .errorCode(HttpStatus.NOT_ACCEPTABLE.toString())
                .build();
        logger.error("{}", build.toString(), e);
        return new ResponseEntity<>(build, HttpStatus.NOT_ACCEPTABLE);
    }

    @ExceptionHandler(value = SystemApiException.class)
    public ResponseEntity<ExceptionInfo> serviceException(SystemApiException e) {
        ExceptionInfo build = ExceptionInfo.create()
                .errorMessage(e.getErrAbstract())
                .errorCode(e.getDetailErrCode())
                .userTip(e.getErrMsg())
                .build();
        logger.error("{}", build.toString(), e);
        return new ResponseEntity<>(build, HttpStatus.SERVICE_UNAVAILABLE);
    }

    @ExceptionHandler(value = DataAccessException.class)
    public ResponseEntity<ExceptionInfo> dataAccessException(DataAccessException e) {
        ExceptionInfo build = ExceptionInfo.create()
                .errorMessage(e.getCause().getMessage())
                .userTip("Dao层发生异常 ")
                .errorCode("A20000")
//                .stackTrace(Arrays.toString(e.getStackTrace()))
                .build();
        logger.error("{}", build.toString(), e);
        return new ResponseEntity<>(build, HttpStatus.SERVICE_UNAVAILABLE);
    }

    @ExceptionHandler(value = Exception.class)
    public ResponseEntity<ExceptionInfo> exception(Exception e) {
        ExceptionInfo build = ExceptionInfo.create()
                .errorMessage(e.getMessage())
                .userTip("Service层发生异常 ")
                .errorCode("A30000")
                .stackTrace(Arrays.toString(e.getStackTrace()))
                .build();
        logger.error("{}", build.toString(), e);
        return new ResponseEntity<>(build, HttpStatus.SERVICE_UNAVAILABLE);
    }
}
